<template>
  <div class="mod-goods">
    <el-form :inline="true" :model="searchForm" @keyup.enter.native="pageIndex = 1;getDataList()">
      <el-form-item>
        <el-select v-model="searchForm.goodsType" placeholder="商品类型">
          <el-option
            label="联联商品"
            :value="2">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item v-show="searchForm.goodsType === 2">
        必填：
        <el-checkbox v-model="searchForm.bookingShowAddress">地址</el-checkbox>
        <el-checkbox v-model="searchForm.orderShowIdCard">身份证</el-checkbox>
        <el-checkbox v-model="searchForm.orderShowDate">游玩时间</el-checkbox>
      </el-form-item>
      <el-form-item>
        <el-button @click="pageIndex = 1; getDataList()">查询</el-button>
        <el-button @click="syncGoods" type="success">同步到本地</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>
      <el-table-column
        width="150"
        prop="name"
        header-align="center"
        align="center"
        label="商品名称">
      </el-table-column>
      <el-table-column
        prop="categoryName"
        header-align="center"
        align="center"
        label="商品类型">
      </el-table-column>
      <el-table-column
        prop="isOnSale"
        header-align="center"
        align="center"
        label="是否上架">
        <template slot-scope="scope">
          <span v-if="scope.row.isOnSale === 0"  size="small">否</span>
          <span v-else-if="scope.row.isOnSale === 1">是</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="goodsNumber"
        header-align="center"
        align="center"
        label="商品库存">
      </el-table-column>
      <el-table-column
        prop="listPicUrl"
        header-align="center"
        align="center"
        label="列表图">
        <template slot-scope="scope">
          <img style="height: 50%;width: 50%" @click="openImg(scope.row.listPicUrl)" :src="scope.row.listPicUrl"/>
        </template>
      </el-table-column>
      <el-table-column
        show-tooltip-when-overflow
        prop="goodsBrief"
        header-align="center"
        align="center"
        label="简明介绍">
      </el-table-column>
      <el-table-column
        prop="unitPrice"
        header-align="center"
        align="center"
        label="进价">
      </el-table-column>
      <el-table-column
        prop="marketPrice"
        header-align="center"
        align="center"
        label="市场价">
      </el-table-column>
      <el-table-column
        prop="retailPrice"
        header-align="center"
        align="center"
        label="零售价格">
      </el-table-column>
      <el-table-column
        prop="counterPrice"
        header-align="center"
        align="center"
        label="专柜价格">
      </el-table-column>
      <el-table-column
        prop="sales"
        header-align="center"
        align="center"
        label="销售量">
      </el-table-column>
      <el-table-column
        prop="sort"
        header-align="center"
        align="center"
        label="排序">
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>

    <el-dialog
      title="同步到本地"
      :visible.sync="dialogVisible"
      width="20%">
      <el-select v-model="categoryId" placeholder="请选择" filterable>
        <el-option
          v-for="item in categoryList"
          :key="item.id"
          :label="item.name"
          :value="item.id">
        </el-option>
      </el-select>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirmSync">确 定</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>

export default {
  data () {
    return {
      goodsId: '',
      searchForm: {
        name: '',
        goodsType: 2,
        bookingShowAddress: false,
        orderShowIdCard: false,
        orderShowDate: false
      },
      categoryId: '',
      categoryList: [],
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListSelections: [],
      dialogVisible: false
    }
  },
  activated () {
    this.activatedMethod()
    this.searchForm.goodsType = 2
  },
  methods: {
    confirmSync(){
      if(this.categoryId === ''){
        this.$message({
          message: '请选择类型',
          type: 'error',
          duration: 1000
        })
        return
      }
      this.$http({
        url: `/mch/goods/syncGoods`,
        method: 'post',
        data: {
          "categoryId": this.categoryId,
          "ids": this.dataListSelections.map(x=>x.id)
        }
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.$message({
            message: '操作成功',
            type: 'success',
            duration: 1000
          })
          this.dialogVisible = false
        }
      })
    },
    syncGoods(){
      if(!(this.dialogVisible = this.dataListSelections.length > 0)){
        this.$message({
          message: '请选择需要同步的商品',
          type: 'error',
          duration: 1000
        })
      }
    },
    activatedMethod(goodsType){
      this.searchForm.goodsType = goodsType
      this.getDataList()
      this.getCategoryList()
    },
    getCategoryList () {
      this.$http({
        url: `/mall/category/queryAll`,
        method: 'get',
        params: {
          'level': 2,
          'isShow': 1
        }
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.categoryList = data.list
        } else {
          this.categoryList = []
        }
      })
    },
    // 获取数据列表
    getDataList () {
      this.$http({
        url: '/mch/goods/list',
        method: 'get',
        params: {
          'page': this.pageIndex,
          'limit': this.pageSize,
          'name': this.searchForm.name,
          'goodsType': this.searchForm.goodsType,
          'bookingShowAddress': this.searchForm.bookingShowAddress + 0,
          'orderShowIdCard': this.searchForm.orderShowIdCard + 0,
          'orderShowDate': this.searchForm.orderShowDate + 0
        }
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.dataList = data.page.records
          this.totalPage = data.page.total
        } else {
          this.dataList = []
          this.totalPage = 0
        }
      })
    },
    // 每页数
    sizeChangeHandle (val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle (val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 多选
    selectionChangeHandle (val) {
      this.dataListSelections = val
    }
  }
}
</script>
